/* nixonFTP
 * FTP client version 0.1
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
*/

package nixonftp;

import java.util.Date;
import java.util.HashMap;

public class NXObjectIndex {

	public final int type;
	public int realType = -1;
	public String name;
	public String permissions;
	public String owner;
	public String group;
	public long size;
	public int hardLinks = 0;
	public Date date;
	public HashMap objects;
	public boolean init;
	public String realLoc;

	public static final int TYPE_FILE = 0;
	public static final int TYPE_LINK = 1;
	public static final int TYPE_DIR = 2;
	public static final int TYPE_DRIVE = 3;
	public static final int TYPE_HOME = 4;

	public static final int OWNER = 0;
	public static final int GROUP = 1;
	public static final int OTHER = 2;

	public static final int READ = 0;
	public static final int WRITE = 1;
	public static final int EXECUTE = 2;

	public NXObjectIndex(char initType, String initName) {
		this(initType, initName, "wrxwrxwrx", "", "", 0, 0, new Date());
	}

	public NXObjectIndex(char initType, String initName, String initPermissions, String initOwner, String initGroup, long initSize, int initHardLinks, Date initDate) {
		switch (initType) {
			case 'l':
				type = TYPE_LINK;
				break;
			case 'd':
				type = TYPE_DIR;
				break;
			case 'z':
				type = TYPE_DRIVE;
				break;
			case 'h': //home item
				type = TYPE_HOME;
				break;
			default:
				type = TYPE_FILE;
		}
		name = initName;
		permissions = initPermissions;
		owner = initOwner;
		group = initGroup;
		size = initSize;
		hardLinks = initHardLinks;
		date = initDate;
		objects = new HashMap<String, NXObjectIndex>();
		init = true;
	}

	public String getKind() {
		switch (type) {
			case TYPE_FILE:
				return "File";
			case TYPE_LINK:
				return "Link";
			case TYPE_DIR:
				return "Folder";
			case TYPE_DRIVE:
				return "Drive";
			case TYPE_HOME:
				return "Home Folder";
		}
		return null;
	}

	public boolean canDo(int person, int permission) {
		if (permissions.isEmpty()) return false;
		int start = 0; char compare = 0;
		switch (person) {
			case OWNER:
				break;
			case GROUP:
				start = 3;
				break;
			case OTHER:
				start = 6;
				break;
		}
		switch (permission) {
			case READ:
				compare = 'r';
				break;
			case WRITE:
				compare = 'w';
				break;
			case EXECUTE:
				compare = 'x';
				break;
		}
		if (permissions.length() <= start + permission) return false;
		return permissions.charAt(start + permission) == compare;
	}

	void setRealLocation(String loc) {
		realLoc = loc;
	}

	void setRealType(int type) {
		realType = type;
	}
}